package me.goldze.mvvmhabit.widget;


import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.Objects;

import me.goldze.mvvmhabit.R;


/**
 * Created by xmlluhuan on 2017/9/25.
 * 网络加载框
 */

public class Loading {
    private Dialog progressDialog;
    private Context context;
    private LinearLayout ll_bg;

    public Loading(Context context) {
        this.context = context;
    }

    //点击dialog外部可以dismiss
    public void show(String msgStr) {
        if (progressDialog == null) {
            progressDialog = new Dialog(context, R.style.progress_dialog);
        }
        progressDialog.setContentView(R.layout.dialog_loading);
        progressDialog.setCancelable(true);
        Objects.requireNonNull(progressDialog.getWindow())
                .setBackgroundDrawableResource(android.R.color.transparent);
        TextView msg = progressDialog.findViewById(R.id.loading_msg_text);
        ll_bg = progressDialog.findViewById(R.id.ll_bg);
//        ll_bg.getBackground().mutate().setAlpha(150);
        msg.setText(msgStr);
        msg.setVisibility(TextUtils.isEmpty(msgStr) ? View.GONE : View.VISIBLE);
        progressDialog.setCanceledOnTouchOutside(true);
        progressDialog.show();
    }

    //点击dialog外部无法dismiss，必须等待完成
    public void showNoHide(String msgStr) {
        if (progressDialog == null) {
            progressDialog = new Dialog(context, R.style.progress_dialog);
        }

        progressDialog.setContentView(R.layout.dialog_loading);
        progressDialog.setCancelable(true);
        progressDialog.setCanceledOnTouchOutside(false);
        Objects.requireNonNull(progressDialog.getWindow())
                .setBackgroundDrawableResource(android.R.color.transparent);
        TextView msg = progressDialog.findViewById(R.id.loading_msg_text);
        ll_bg = progressDialog.findViewById(R.id.ll_bg);
//        ll_bg.getBackground().mutate().setAlpha(150);
        msg.setText(msgStr);
        msg.setVisibility(TextUtils.isEmpty(msgStr) ? View.GONE : View.VISIBLE);
        progressDialog.show();
    }

    //dismiss dialog
    public void cancel() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

}